// 格式化时间
export function formatTime(date) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : `0${n}`
}

// Toast提示
export function showToast(title, icon = 'none', duration = 2000) {
    uni.showToast({
        title,
        icon,
        duration
    })
}

// Loading
export function showLoading(title = '加载中...') {
    uni.showLoading({
        title,
        mask: true
    })
}

export function hideLoading() {
    uni.hideLoading()
}

// 模态框
export function showModal(content, title = '提示') {
    return new Promise((resolve, reject) => {
        uni.showModal({
            title,
            content,
            success: res => {
                if (res.confirm) {
                    resolve(true)
                } else {
                    resolve(false)
                }
            },
            fail: err => {
                reject(err)
            }
        })
    })
}
